﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Geometry;

namespace cadbom
{
    public partial class LoginForm : Form
    {
        public string domainid = "...";
        public bool enablelogin = false;
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor editor = doc.Editor;
            string password = EncryptHelper.Encrypt2(textBox2.Text.Trim(), "1234567890adbcde", "1234567890hjlkew");

            string enpd = EncryptHelper.Decrypt2(password, "1234567890adbcde", "1234567890hjlkew");
            string postData = "{\"username\":\"" + textBox1.Text.Trim() + "\",\"password\":\"" + password + "\",\"captchaFlag\":\"true\"}";
            
            Http.CreateHttpClient(GValues.domainLink + "sys/login", GValues.tenant, GValues.token).PostAsync(postData, new Action<Http.HttpResp>((r) =>
            {
                var responCode = Convert.ToInt32(r.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResult<LoginDTO> responResult = JsonConvert.DeserializeObject<ResponResult<LoginDTO>>(r.AckStr());
                    if (responResult.success)
                    {
                        GValues.account = textBox1.Text;
                        GValues.password = StringSecurity.DESEncrypt(textBox2.Text); ;
                        GValues.token = responResult.result.token;
                        SqlLiteUtils sqlLite = new SqlLiteUtils();
                        sqlLite.createDb();
                        sqlLite.insertData(GValues.account, GValues.password, GValues.tenant, GValues.token);
                        this.Close();
                    }
                    else
                    {
                        editor.WriteMessage(responResult.code + " " + responResult.message);
                    }
                }
                else
                {
                    editor.WriteMessage(r.Code + " " + " " + responCode + " " + r.Error);
                }
            }));
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void LoginForm_Load(object sender, EventArgs e)
        {

        }
        private void initUserInfo(String account, String password)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
